package com.keepers.repair.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.keepers.repair.R;
import com.keepers.repair.adapter.TaskMaintenanceAdapter;
import com.keepers.repair.helper.SpHelper;
import com.keepers.repair.model.OverUserModel;
import com.keepers.repair.model.TaskMaintenanceModel;
import com.keepers.repair.model.UserModel;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.utils.CollectionUtil;
import com.keepers.repair.utils.DefaultUtils;
import com.keepers.repair.utils.UserRoleType;
import com.keepers.repair.utils.UtilsKt;
import com.keepers.repair.view.PullRecyclerView;
import com.keepers.repair.view.TitleBar;
import com.keepers.repair.view.picker.SinglePickerDialog;
import com.keepers.repair.view.picker.info.PickerInfo;

import java.util.List;

/**
 * auth aboom
 * date 2018/11/17
 */
public class MaintenanceActivity extends BaseActivity {

    private PullRecyclerView       recyclerView;
    private UserModel              loginUserInfo;
    private TaskMaintenanceAdapter taskMaintenanceAdapter;
    private TextView               emptyView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintenance);
        TitleBar titleBar = f(R.id.title_bar);
        titleBar.initializeHeader("检修任务");
        emptyView = f(R.id.empty_view);
        loginUserInfo = SpHelper.getLoginUserInfo();
        recyclerView = f(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setOnRefreshListener(this::reload);
        taskMaintenanceAdapter = new TaskMaintenanceAdapter(R.layout.item_task);
        taskMaintenanceAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            TaskMaintenanceModel item = taskMaintenanceAdapter.getItem(position);
            if (item == null) {
                return;
            }
            rxHelper.pend(DataLoader.getOverhaulList(loginUserInfo.Gid, loginUserInfo.GroupGid, UserRoleType.MAINTENANCE.getType(), userListModel -> {
                if (userListModel == null) {
                    AlertToast.show("服务器异常...");
                    return;
                }
                List<OverUserModel> userList = userListModel.UserList;
                if (CollectionUtil.isEmpty(userList)) {
                    AlertToast.show("没有获取到相应的检修人员");
                    return;
                }
                List<PickerInfo> pickers = UtilsKt.convertItems(userList, overUserModel -> new PickerInfo(overUserModel.Name));
                new SinglePickerDialog(this)
                        .setPickerTitle("任务分配")
                        .setPickers(pickers)
                        .setListener((p, pickerData) -> {
                            OverUserModel overUserModel = userList.get(p);
                            rxHelper.pend(DataLoader.makeRepair(loginUserInfo.Gid, loginUserInfo.GroupGid, item.GPSID, overUserModel.Gid, item.TaskName, o -> {
                                if (DefaultUtils.value(o)) {
                                    AlertToast.show("任务分配给" + overUserModel.Name);
                                    reload();
                                } else {
                                    AlertToast.show("分配失败");
                                }
                            }));
                        })
                        .show();
            }));

//            DialogUtil.showCommonDialog(MaintenanceActivity.this, "任务", "领取此任务？"
//                    , (dialog, which) -> {
//                    }, null);
        });
        recyclerView.setAdapter(taskMaintenanceAdapter);
        reload();

    }

    private void reload() {
        recyclerView.showRefresh();
        rxHelper.pend(DataLoader.getRepairTaskList(loginUserInfo.Gid, loginUserInfo.GroupGid, o -> {
            recyclerView.loadComplete(true);
            if (o == null || o.TableList == null) {
                AlertToast.show("获取任务失败...");
                return;
            }
            taskMaintenanceAdapter.setNewData(o.TableList);
            emptyView.setVisibility(o.TableList.size() == 0 ? View.VISIBLE : View.GONE);
        }));
    }
}
